package com.findmeastore.action;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;

import com.findmeastore.bc.IBusinessDetailsBC;
import com.findmeastore.bo.BrandBo;
import com.findmeastore.bo.BusinessEntity;
import com.findmeastore.bo.ProductBo;
import com.findmeastore.bo.RelBusinessBrand;
import com.findmeastore.bo.RelBusinessProduct;
import com.findmeastore.dto.RelBusinessProdBrandDto;
import com.opensymphony.xwork2.ActionSupport;

public class ProductBrandRelAction extends ActionSupport {
	private Integer businessId;
	private List<RelBusinessProdBrandDto> relBusProdBrandList = new ArrayList<RelBusinessProdBrandDto>();
	
	private IBusinessDetailsBC businessDetailsBc;
	
	public String execute(){
		return "success";
	}
	
	public String editProdBrandRel(){
		if(businessId == null && businessId < 1) {
			throw new RuntimeException("Invalid business id");
		}
		
		BusinessEntity entity = businessDetailsBc.getBusinessById(businessId);
		
		if(entity == null) {
			throw new RuntimeException("Invalid input");
		}
		Set<RelBusinessProduct> relBusinessProducts = entity.getRelBusinessProducts();
		
		if(CollectionUtils.isNotEmpty(relBusinessProducts)) {
			// preparing existing selected Brands Ids
			for(RelBusinessProduct relBusinessProduct : relBusinessProducts) {
				ProductBo product = relBusinessProduct.getMasterProduct();
				RelBusinessProdBrandDto dto = new RelBusinessProdBrandDto();
				dto.setProductName(product.getName());
				dto.setBusinessProdRelId(relBusinessProduct.getId());
				dto.setBusinessId(entity.getId());
				dto.setCategoryId(relBusinessProduct.getCategoryId());
				dto.setProductId(product.getId());
				dto.setAllBrands(product.getBrands());
				Set<BrandBo> selectedBrands = new HashSet<BrandBo>();
				Set<RelBusinessBrand> relBusinessBrands = relBusinessProduct.getRelBusinessBrands();
				for(RelBusinessBrand relBusinessBrand : relBusinessBrands) {
					selectedBrands.add(relBusinessBrand.getMasterBrand());
				}
				dto.setSelectedBrands(selectedBrands);
				relBusProdBrandList.add(dto);
			}
		}
		return SUCCESS;
	}
	
	public String save() {
		if(businessId == null || businessId < 1) {
			throw new RuntimeException("Invalid input");
		}
		
		BusinessEntity entity = businessDetailsBc.getBusinessById(businessId);
		if(entity == null) {
			throw new RuntimeException("Invalid business id");
		}
		// hard coded last modified by id
		businessDetailsBc.saveBusinessBrandsRel(relBusProdBrandList, 1);
		return SUCCESS;
	}

	public Integer getBusinessId() {
		return businessId;
	}

	public void setBusinessId(Integer businessId) {
		this.businessId = businessId;
	}

	public List<RelBusinessProdBrandDto> getRelBusProdBrandList() {
		return relBusProdBrandList;
	}

	public void setRelBusProdBrandList(
			List<RelBusinessProdBrandDto> relBusProdBrandList) {
		this.relBusProdBrandList = relBusProdBrandList;
	}

	public IBusinessDetailsBC getBusinessDetailsBc() {
		return businessDetailsBc;
	}

	public void setBusinessDetailsBc(IBusinessDetailsBC businessDetailsBc) {
		this.businessDetailsBc = businessDetailsBc;
	}

}
